#ifndef CAMERADAEMON_H
#define CAMERADAEMON_H

#include <semaphore.h>
#include <pthread.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <string>

#include "timestamp.h"

#define CAMERA_PROP_COUNT   40
#define CAMERA_WIDTH_ID     CV_CAP_PROP_FRAME_WIDTH
#define CAMERA_HEIGHT_ID    CV_CAP_PROP_FRAME_HEIGHT
#define CAMERA_BRIGHTNESS_ID    CV_CAP_PROP_BRIGHTNESS
#define CAMERA_CONTRAST_ID  CV_CAP_PROP_CONTRAST
#define CAMERA_SATURATION_ID    CV_CAP_PROP_SATURATION
#define CAMERA_HUE_ID       CV_CAP_PROP_HUE
#define CAMERA_FOCUS_ID     -1
class CameraDaemon
{
public:
    CameraDaemon();
    ~CameraDaemon();
    bool startCapture(int device = -1, int fps = 30);
    bool startCapture(std::string file, int fps = 30);
    bool getImage(cv::Mat& img, TimeStamp& t);
    void stopCapture();
    friend void* captureThread(void* _cam);
    bool setProp(int id, double v);
    double getProp(int id);
    void startRecord();
    void stopRecord();
    bool getRecordState();


private:
    //for property
    void checkProp();
    bool m_prop[CAMERA_PROP_COUNT];
    sem_t m_prop_mutex;
    int m_width;
    int m_height;
    double m_brightness;
    double m_contrast;
    double m_saturation;
    double m_hue;
    int m_focus;

    //for capturing
    bool m_from_file;
    int m_fps;
    int m_interval;
    std::string m_file;
    int m_device;
    void capture();
    pthread_t m_capture_thread;
    bool m_working;
    cv::VideoCapture m_capture;
    sem_t m_buffer_mutex;
    TimeStamp m_buffer_time;
    cv::Mat m_buffer;

    //for recording
#define RECORD_FOLDER "./record/"
    bool m_need_record;
    bool m_recording;
    std::string m_record_file;
    cv::VideoWriter m_record_writer;
    sem_t m_record_mutex;



};

#endif // CAMERADAEMON_H
